package utils

import (
	"cyiris/core/core"
	"cyiris/core/protocol/gpb"
	"time"

	log "cyiris/core/zaplog"

	"google.golang.org/protobuf/proto"
)

func Send2Demo(serviceName string, body []byte) (err error) {
	exchangeName := "cyiris-test-exchange"
	routingKey := "chenishr"

	header := gpb.Header{
		Id:          12323,
		Type:        gpb.Type_CON,
		ServiceName: serviceName,
		Version:     "1.0.1",
		CreateTime:  time.Now().Unix(),
		Extend:      "",
	}

	msg := gpb.Message{
		Header: &header,
		Body:   body,
	}

	data, err := proto.Marshal(&msg)
	if err != nil {
		log.Errorf("marshaling error: ", err)
		return err
	}

	pub, err := core.GetMqPublisher(exchangeName)
	if err != nil {
		log.Errorf(err.Error())
		return err
	}

	err = pub.Push(routingKey, data)

	return err
}
